//此文件与HighLight.css一同使用,用于高亮显示html+css+JavaScript代码；
function highlight(code, codeType) { //参数列表为需要修改的元素，代码格式(HTML,CSS,JavaScript)

	// code = code.replace(/</ig, "&lt;");
	// code = code.replace(/>/ig, "&gt;");
	var codes = code.split("\n");
	code = null;
	var spe = {
		"'": ";a;",
		"=": ";b;",
		"class": ";c;"
	}; //实体字符
	var type = {
		'html': new Array(),
		'css': new Array(),
		'JavaScript': new Array()
	};
	if (codeType == null) {
		type = splitCode(codes);
	} else {
		if (codeType == "js" || codeType == "javascrpit") {
			codeType = "JavaScript";
		}
		if (codeType == "HTML" || codeType == "Html") {
			codeType = "html";
		}
		if (codeType == "CSS" || codeType == "Css") {
			codeType = "css";
		}
		type[codeType] = codes;
	}
	if (codeType == null) {
		HLcode(type["html"], "html");
		HLcode(type['css'], "css");
		HLcode(type["JavaScript"], "JavaScript");
	} else {
		HLcode(type[codeType], codeType);
	}
	if (codeType == null) {
		codes = connectCode(type);
	}
	var inHTML = "";
	for (var i in codes) {
		inHTML += codes[i] + "</br>";
	}
	return `<pre class="code">${inHTML}<code>`;
	function splitCode(codes) { //分离html,css,js代码
		var type = {
			'html': new Array(),
			'css': new Array(),
			'JavaScript': new Array()
		};

		var flag = 0;
		for (var c = 0; c < codes.length; c++) { //提取js代码
			if (codes[c].match("script") != null) {
				if (flag == 0) {
					flag = 1;
					continue;
				} else {
					flag = 0;
					break;
				}
			}
			if (flag == 1) {
				type["JavaScript"].push(codes[c]);
				codes[c] = "";
			}
		}
		for (var c = 0; c < codes.length; c++) { //提取CSS代码
			if (codes[c].match("style") != null) {
				if (flag == 0) {
					flag = 1;
					continue;
				} else {
					flag = 0;
					break;
				}
			}
			if (flag == 1) {
				type['css'].push(codes[c]);
				codes[c] = "";
			}
		}
		for (var c = 0; c < codes.length; c++) { //提取Html代码
			if (codes[c] != "") {
				type["html"].push(codes[c]);
			}
		}
		return type;
	}
	function HLcode(codes, CodeType) { //高亮代码 ，这个是关键部分
		var keyword = ["break", "else", "new", "var", "case", "finally", "return", "void", "catch", "for", "switch", "while", "continue", "function", "this", "with", "default", "if", "throw", "delete", "in", "try", "do", "instranceof", "typeof"];
		var ke = function (word) {
			var k = "";
			for (i in keyword) {
				k += keyword[i] + "[^A-z]";
				if (i != keyword.length - 1) {
					k += "|";
				}
			}
			return k;
		}(keyword);
		var redundants = {
			"html": ["/", "&lt;", "!", '&gt;'],
			"css": ['{', ';', ':'],
			"JavaScript": ["(", ')', ';']
		}; //多余字符
		var regulars = { //正则
			"html": {
				// "tag": /&lt;\w+&gt;|&lt;\w+\s|[/]\w+&gt;|&lt;!\w+\s/ig,
				"tag": /<\w+>F|<\w+\s|[/]\w+>F|<!\w+\s/ig,
				"str": /\".+\"|\'.+\'/ig,
				"noun": /\w+(?==)/ig,
				"num": /\d+|#\w(?=;)/ig
			},
			"css": {
				"id_class": /#{0,1}\w+\s{0,}{|\.\w+(?=,)/ig,
				"noun": /\s{0,1}\w{2,}[-]{0,1}\w+(?=:)|px|em|%|:\s{0,1}\w+\s{0,1}(?=;)|:.+;|{\w+(?==)/ig,
				"num": /\s{0,}#\w+\s{0,};|\s{0,}\d+\s{0,}/ig
			},
			"JavaScript": {
				"str": /\".+\"|\'.+\'/ig,
				"num": /\s{0,}#\w+\s{0,};|\s{0,}\d+\s{0,}/ig,
				"noun": new RegExp(ke, "ig"),
				"fun": /\w+\.{0,}\w+\.{0,}\w+\.{0,}\w+\.{0,}\w+\.{0,}\w+\(/ig,
				"tag": /=/ig
			}

		}
		var red = redundants[CodeType];
		var regs = regulars[CodeType];
		for (var c in codes) { //遍历语句
			// 这个是之前的方式,太过局限
			// for (var r in regs) { //遍历正则名
			// 	var lines = code[c].match(regs[r]); //提出符合要求的子串
			// 	if (lines == null) {
			// 		continue;
			// 	}
			// 	for (var l in lines) {
			// 		if (lines[l][0] == "'" || lines[l][0] == '"') { //特殊情况，，，，我也很无奈啊
			// 			var chge = "<span " + spe["class"] + spe["="] + spe["'"] + r + spe["'"] + ">" + lines[l] + "</span>";
			// 			code[c] = code[c].replace(lines[l], chge);
			// 			continue;
			// 		}
			// 		for (x in red) {
			// 			lines[l] = lines[l].replace(red[x], "");
			// 		}
			// 		var chge = "<span " + spe["class"] + spe["="] + spe["'"] + r + spe["'"] + ">" + lines[l] + "</span>";
			// 		code[c] = code[c].replace(new RegExp(lines[l], "ig"), chge);
			// 	}
			// 	if (code[c].indexOf("href") != -1) {
			// 		console.log(r, lines, code[c]);
			// 	}
			// }

			//2.0版本
			let posistion = [
				// { start,end,className,string }//开始和结束的位置，需要添加的类名，需要插入的字符串
			]

			for (let r in regs) {
				let index = codes[c].indexOf(regs[r]);
				if (index != -1) {
					var str = codes[c].match(regs[r]).length; //提出符合要求的子串
					if (r == "str") {
						console.log(str);
						codes[c].replace(str, "");
						posistion.push({
							start: index,
							end: index + 1,
							className: r,
							string: str
						})
					}
				}
			}
		}
		for (var c in codes) {
			for (var i in spe) {
				codes[c] = codes[c].replace(new RegExp(spe[i], "ig"), i);
			}
		}
	}
	function connectCode(type) { //组合html,css,js代码
		var codes = new Array();
		var cssFlag = 0;
		var jsFlag = 0;
		for (var i in type["html"]) {
			codes.push(type["html"][i]);
			if (type["html"][i].match("style") != null && cssFlag == 0) {
				cssFlag = 1;
				for (cs in type['css']) {
					codes.push(type['css'][cs]);
				}
			}
			if (type["html"][i].match("script") != null && jsFlag == 0) {
				jsFlag = 1;
				for (js in type["JavaScript"]) {
					codes.push(type["JavaScript"][js]);
				}
			}
		}
		return codes;
	}
}